﻿using System;
using System.IO;

namespace BunnyEater.Common.Jwt
{
    /// <summary>
    /// JWT密钥管理类，用于读取和管理JWT相关的密钥
    /// </summary>
    public class JwtSecret
    {
        /// <summary>
        /// 读取txt文件，获取账户令牌私钥(随机字符串)
        /// 秘钥长度必须：>=16位
        /// </summary>
        /// <returns>账户令牌私钥</returns>
        public static string GetAccountKey()
        {
            //设置[公钥私钥]文件路径
            string privateKeyPath = @"privateKey.txt";
            var privateKey = GetSecurityKey(privateKeyPath);
            return privateKey;
        }

        /// <summary>
        /// 读取txt文件，获取刷新令牌私钥(随机字符串) 
        /// 秘钥长度必须：>=16位
        /// </summary>
        /// <returns>刷新令牌私钥</returns>
        public static string GetRefreshKey()
        {
            //设置[公钥私钥]文件路径
            string refreshKeyPath = @"refreshKey.txt";
            var refreshKey = GetSecurityKey(refreshKeyPath);
            return refreshKey;
        }

        /// <summary>
        /// 从指定路径读取安全密钥
        /// 秘钥长度必须：>=16位
        /// </summary>
        /// <param name="tmpPath">密钥文件路径</param>
        /// <returns>安全密钥</returns>
        private static string GetSecurityKey(string tmpPath)
        {
            //设置[公钥私钥]文件路径
            string fullName = Environment.CurrentDirectory;
            var fullPath = fullName.Substring(0, fullName.LastIndexOf("\\")) + "\\BunnyEater.WebApi\\wwwroot\\jwtkey\\" + tmpPath;
            var securityKey = File.ReadAllText(fullPath);
            return securityKey;
        }
    }
}